package cn.edu.cug.cs.gtl;

import cn.edu.cug.cs.gtl.protos.*;
import cn.edu.cug.cs.gtl.sql.executor.SQLExecutor;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import java.awt.*;
import java.util.Vector;

/**
 * 原始数据界面，在菜单栏点击"原始数据"后显示
 */
public class RawDataFrame extends JFrame {
    String url="jdbc:mysql://127.0.0.1:3306/yuman?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    String username="root";
    String password="sjh020401";

    String rawDataBaseUrl;
    String rawDataBaseUser;
    String rawDataBasePassword;
    JComboBox<String> dataSourceComboBox;
    JTree tree;
    DefaultMutableTreeNode rawDataNode;
    JPanel leftPanel;//左边，放下拉框和树
    JTable rawDataTable;
    JScrollPane rawDataScrollPane;

    public RawDataFrame(){
        //初始化窗口
        setBounds(300,100,800,600);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        leftPanel=new JPanel();
        leftPanel.setLayout(new BorderLayout());
        leftPanel.setPreferredSize(new Dimension(150,this.getHeight()));
        dataSourceComboBox=new JComboBox<String>();
        //创建树
        createTree();
        //创建数据源下拉框
        createDataSourceComboBox();
        //初始化表格
        InitialTable();

        //tree控件监听
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent evt) {

            }
        });
        this.getContentPane().add(leftPanel,BorderLayout.WEST);
        this.getContentPane().add(rawDataScrollPane,BorderLayout.CENTER);
    }

    /**
     * 创建数据源下拉框
     */
    private void createDataSourceComboBox() {
        try{
            TableKey tableKey = TableKey.newBuilder()
                    .setUrl(url)
                    .setUserName(username)
                    .setPassword(password)
                    .setSchemaName("yuman")
                    .setTableName("sys_datasource")
                    .build();


            //WHERE字句条件设置
            Parameters psWhere = Parameters.newBuilder()
                    .addParameter(Parameter.newBuilder().setName("type").setValue("0"))
                    .build();

            Tuples p = SQLExecutor.select(tableKey,psWhere);
            //无数据源
            if(p.getSerializedSize()==0){
                dataSourceComboBox.addItem("无");
                return;
            }
            //初始化数据库信息
            rawDataBaseUrl=p.getTuple(0).getValue(2);
            rawDataBaseUser=p.getTuple(0).getValue(3);
            rawDataBasePassword=p.getTuple(0).getValue(4);
            //插入
            for(int i=0;i<p.getTupleCount();i++){
                dataSourceComboBox.addItem(p.getTuple(i).getValue(0));
            }
            leftPanel.add(dataSourceComboBox,BorderLayout.CENTER);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 创建树
     */
    private void createTree() {
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
        //创建二级节点
        rawDataNode = new DefaultMutableTreeNode("原始数据");

        // 把二级节点作为子节点添加到根节点
        rootNode.add(rawDataNode);

        //创建其它子节点
        addChildrenNode(rawDataNode);

        // 使用根节点创建树组件
        tree = new JTree();
        TreeModel treeModel = new DefaultTreeModel(rootNode);
        tree.setModel(treeModel);
        tree.setRootVisible(false);

        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(150,this.getHeight()-100));
        leftPanel.add(scrollPane,BorderLayout.SOUTH);
    }

    /**
     * 给树节点添加子节点
     * @param parsentNode 父节点
     */
    private void addChildrenNode(DefaultMutableTreeNode parsentNode) {

    }

    /**
     * 初始化表格
     */
    private void InitialTable() {
        rawDataTable=new JTable();
        //查询表头
        try {
            TableKey tableKey = TableKey.newBuilder()
                    .setUrl(url)
                    .setUserName(username)
                    .setPassword(password)
                    .setSchemaName("yuman")
                    .setTableName("raw_data")
                    .build();

            TableInfo tableInfo = SQLExecutor.getTableInfo(tableKey);
            String []columnName=new String[tableInfo.getColumnInfoCount()];
            for (int i = 0; i < tableInfo.getColumnInfoCount(); i++) {
                ColumnInfo columnInfo=tableInfo.getColumnInfo(i);
                columnName[i]=columnInfo.getName();
            }
            String [][]data={};
            DefaultTableModel defaultTableModel=new DefaultTableModel(data,columnName);
            rawDataTable.setModel(defaultTableModel);
            rawDataScrollPane=new JScrollPane(rawDataTable);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
